package com.han.jdbc.mybatis._02.mapppers;
/**
* @author hanpeng
* @version 创建时间：2020年7月18日 下午1:02:09
* @ClassName 类名称
* @Description 类描述
*/

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import com.han.jdbc.mybatis.bean.Emp;

public class MappersTest {
	@Test
	public void testGetEmpByEId() {
		String resource="02mybatis-config-mappers.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = mapper.getEmpByEId(1);
			System.out.println(emp);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			sqlSession.close();
		}
	}
	@Test
	public void testGetEmps() {
		String resource="02mybatis-config-mappers.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession();
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			List<Emp> emps = mapper.getEmps();
			System.out.println(emps.size());
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(sqlSession!=null) {
					sqlSession.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	@Test
	public void testAdd() {
		String resource="02mybatis-config-mappers.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession(true);
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = new Emp();
			emp.setAge(11);
			emp.seteName("xijinp");
			emp.setSex("女");
			mapper.addEmp(emp);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(sqlSession!=null) {
					sqlSession.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	@Test
	public void testUpdate() {
		String resource="02mybatis-config-mappers.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession(true);
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Emp emp = new Emp();
			emp.seteId(1);
			emp.setAge(10);
			emp.seteName("xijinpsa");
			emp.setSex("女mid");
			mapper.updateEmp(emp);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(sqlSession!=null) {
					sqlSession.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	@Test
	public void testDel() {
		String resource="02mybatis-config-mappers.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession(true);
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Integer delete = mapper.delete(7);
			System.out.println(delete);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(sqlSession!=null) {
					sqlSession.close();
				}
			} catch (Exception e2) {
			}
		}
	}
	@Test
	public void testPlugin() {
		String resource="02mybatis-config-mappers.xml";
		SqlSession sqlSession=null;
		try {
			InputStream is = Resources.getResourceAsStream(resource);
			SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
			sqlSession = sqlSessionFactory.openSession(true);
			EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
			Integer delete = mapper.delete(7);
			System.out.println(delete);
		} catch (IOException e) {
			e.printStackTrace();
		}finally {
			try {
				if(sqlSession!=null) {
					sqlSession.close();
				}
			} catch (Exception e2) {
			}
		}
	}
}
